package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;
    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }


    /**
     * @author:gongjiawei
     * @Date:2024/4/8
     *
     * 新增员工
     */
    @PostMapping
    @ApiOperation(value = "新增员工")
    public Result<String> addNewEmployee(@RequestBody EmployeeDTO employeeDTO){
        log.info("获取的结果是：{}",employeeDTO);
        employeeService.addNewEmployee(employeeDTO);
        return Result.success();
    }


    /*
    * @author:gongjiawei
    * @Date:2024/4/8
    *
    * 员工分页查询
    * */
    @GetMapping("/page")
    @ApiOperation("分页查找员工")                   //注意这里的数据不是json类型的，而是query类型既？1？1的类型，这里只要和请求参数的名称一致，就会自动封装数据
    public Result<PageResult> findEmployeePageQuery(EmployeePageQueryDTO employeePageQueryDTO){
log.info("请求的数据是:{} ",employeePageQueryDTO);
        PageResult employeePageQuery = employeeService.findEmployeePageQuery(employeePageQueryDTO);
        return Result.success(employeePageQuery);
    }

/*
* @author:gongjiawei
* @Date:2024/4/8
* 员工的启用以及禁用
* */
@PostMapping("/status/{status}")
    public  Result startOrStop(@PathVariable Integer status,long id){
        employeeService.startOrStop(status,id);
      return Result.success();
    }

    /*
     * @author:gongjiawei
     * @Date:2024/4/8
     * 根据id查找员工信息
     * */

    @GetMapping("/{id}")
    @ApiOperation("根据id查找员工信息")
    public  Result<Employee> findById(@PathVariable long id){
        Employee byId = employeeService.findById(id);
        return Result.success(byId);
    }

    /*
     * @author:gongjiawei
     * @Date:2024/4/8
     * 根据id查找员工信息
     * */

    @PutMapping
    @ApiOperation("根据id查找员工信息")
    public  Result updateEmployee(@RequestBody EmployeeDTO employeeDTO){
       employeeService.updateEmployee(employeeDTO);
        return Result.success();
    }

    /*
     * @author:gongjiawei
     * @Date:2024/4/9
     * 修改密码
     * */

    @PutMapping("/editPassword")
    @ApiOperation("修改密码")
    public  Result updatePassword(@RequestBody PasswordEditDTO passwordEditDTO){
        employeeService.updatePassword(passwordEditDTO);
        return Result.success();
    }




    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation(value = "员工退出")
    public Result<String> logout() {
        return Result.success();
    }

}
